/*
 * @Author: 这个熊
 * @Date: 2022-04-22 16:58:32
 * @LastEditors: 这个熊
 * @LastEditTime: 2022-04-24 09:37:30
 * @Description: 
 */

const request = require('request')


const httpRequest = (ctx) => {
    return new Promise((resolve, reject) => {
        delete ctx.request.header.host
        const { headers = {} } = ctx.request 
        const { url, method } = ctx.request.query
        const bodyData = ctx.request.body
        // console.log(url, method, headers);
        request(
            {
                url,
                method,
                headers,
                body: JSON.stringify(bodyData)
            }, 
            function(error, response, body) {
                if (!error && response.statusCode == 200) {
                    resolve(body)
                }
                else {
                    reject(error)
                }
            }
        )

    })
}


module.exports = async(ctx) => {
    try {
        const body = await httpRequest(ctx)
        ctx.body = body
    } 
    catch (error) {
        ctx.body = error
    }
}